<?php
/**
 * 用户中心逻辑处理
 * User：LLW
 * Date：2020-03-23
 */

namespace app\api\model;

use think\Db;
use think\Model;
use think\response\Json;
use think\Session;

class UserModel extends Model
{
    protected $table = 'cd_user';

    /**
     * 我的页面
     * @return array
     */
    public function getFind()
    {
        $uid = Session::get('uid');
        $find = $this->where('us_uid', $uid)->find();
        $find['ban'] = tpConfig('Ban');
        if (empty($find['us_head'])) {
            $find['us_head'] =  $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/uploads/config/head.jpg';
        }
        $find['us_closed_code'] =  $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$find['us_closed_code'];
        $find['us_head'] =  $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$find['us_head'];
        $find['us_atime'] = date('Y-m-d',$find['us_atime']);
        $find['count'] = \db('raise_user')->where('ru_uid',$uid)->count();
        if ($find) {
            AjaxReturn(1, '成功', $find);
        } else {
            AjaxReturn(-1, '未获取到会员信息');
        }
    }

    /**
     * 获取个人信息
     * @return Json
     */
    public function getMyfind()
    {
        $uid = Session::get('uid');
        $find = $this->where('us_uid',$uid)->find();
        $find['us_head'] =  $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$find['us_head'];
        $find['us_img_code'] =  $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/public/'.$find['us_img_code'];



        AjaxReturn(1,'OK',$find);
    }

    /**
     * 忘记密码
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function post_forget_pwd()
    {
        //初始化相关数据
        $data = input('post.');

        //获取用户信息
        $find = $this->where('us_phone',$data['us_phone'])->find();
        if(empty($find)){
            AjaxReturn(-1,'信息获取失败，请退出从新登录');
        }
        if(empty($data['us_pwd']) || empty($data['us_pwds']) ){
            AjaxReturn(-1,'新密码不可为空');
        }
        if($data['us_pwd'] != $data['us_pwds']){
            AjaxReturn(-1,'两次密码不一致');
        }
        //判断验证码得有效性
        $code = Db::name('code_time')->where(['ct_code'=>$data['code'],'ct_phone'=>$data['us_phone']])->find();
        if(empty($code)){
            AjaxReturn(-1,'验证码不正确');
        }
        if(time() > $code['ct_ltime']){
            AjaxReturn(-1,'验证码已过期');
        }
        $date['us_pwd'] = md5($data['us_pwd']);


        //操作数据
        $res = $this->where('us_uid',$uid)->update($date);
        if($res){
            Session::set('uid','');
            AjaxReturn(-2,'修改成功,请重新登录');
        }else{
            AjaxReturn(-1,'修改失败');
        }
    }
    /**
     * 修改密码
     * @param $data['type'] 1:登录密码
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function postPwd()
    {
        //初始化相关数据
        $data = input('post.');
        $uid = Session::get('uid');

        //获取用户信息
        $find = $this->where('us_uid',$uid)->find();
        if(empty($find)){
            AjaxReturn(-1,'信息获取失败，请退出从新登录');
        }
        if(empty($data['us_ypwd']) || empty($data['us_pwd']) ){
            AjaxReturn(-1,'密码不可为空');
        }
        if($data['us_pwd'] != $data['us_pwds']){
            AjaxReturn(-1,'两次密码不一致');
        }

        if($find['us_pwd'] != md5($data['us_ypwd'])){
            AjaxReturn(-1,'原密码不正确');
        }
        $date['us_pwd'] = md5($data['us_pwd']);


        //操作数据
        $res = $this->where('us_uid',$uid)->update($date);
        if($res){
            Session::set('uid','');
            AjaxReturn(1,'修改成功');
        }else{
            AjaxReturn(-1,'修改失败');
        }
    }

    /**
     * 编辑账户
     * @return Json
     */
    public function postEdit()
    {
        $data = input('post.');
        $uid = Session::get('uid');
        if($data['us_closed_code']){
            $insert['us_closed_code'] = $data['us_closed_code'];
        }
        if($data['us_head']){
            $insert['us_head'] = $data['us_head'];
        }
        if($data['us_closed_href']){
            $insert['us_closed_href'] = $data['us_closed_href'];
        }
        if($data['us_name']){
            $insert['us_name'] = $data['us_name'];
        }
        $res = $this->where('us_uid',$uid)->update($insert);
        if($res){
            AjaxReturn(1,'修改成功');
        }else{
            AjaxReturn(-1,'未做更改');
        }
    }

    /**
     * 修改手机号
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @return Json
     */
    public function postPhone()
    {
        //初始化数据
        $uid = Session::get('uid');
        $phone = input('phone');
        $code = input('code');
        $member = $this->where('us_uid',$uid)->find();
        $mem_phone = $this->where('us_phone',$phone)->find();
        $find = Db::name('code_time')->where(['ct_code'=>$code,'ct_phone'=>$member['us_phone']])->find();
        //安全验证
        if(empty($find)){
            AjaxReturn(-1,'验证码错误');
        }
        if($find['ct_ltime'] < time()){
            AjaxReturn(-1,'验证码已过期');
        }
        if($phone == $member['us_phone']){
            AjaxReturn(-1,'新手机号与原手机号相同');
        }
        if(!empty($mem_phone)){
            AjaxReturn(-1,'此手机号已被注册，您可申请找回密码');
        }
        //通过验证改变数据
        $res = $this->where('us_uid',$uid)->update(['us_phone'=>$phone]);
        if($res){
            //记录操作信息
            accountLog(2,2,$member['us_phone'],'修改手机号,原号码:'.$member['us_phone'].'，新号码:'.$phone);
            AjaxReturn(1,'修改成功');
        }else{
            AjaxReturn(-1,'修改失败');
        }
    }

    /**
     * 消息列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLogs()
    {
        $where['ul_uid'] = Session::get('uid');
        input('key') ? $where['ul_desc'] = array('like',"%".input('key')."%") : false;
        $list = Db::name('user_log')->where($where)->where('ul_state',0)->order('ul_time desc')->select();
        $lists = Db::name('user_log')->where($where)->where('ul_state',1)->order('ul_time desc')->select();
        foreach ($list as $k=>$v){
            $str = mb_strlen($v['ul_desc'],'utf-8');
            if($str > 15){
                $list[$k]['ul_desc'] = mb_substr($v['ul_desc'], 0, 8, 'utf-8').'....';
            }
            $list[$k]['ul_time'] = date('Y-m-d H:i:s',$v['ul_time']);
        }
        foreach ($lists as $key=>$val){
            $strs = mb_strlen($val['ul_desc'],'utf-8');
            if($strs > 20){
                $lists[$key]['ul_desc'] = mb_substr($val['ul_desc'], 0, 8, 'utf-8').'....';
            }
            $lists[$key]['ul_time'] = date('Y-m-d H:i:s',$val['ul_time']);
        }
        $arr = array('wei'=>$list,'yi'=>$lists);
        AjaxReturn(1,'ok',$arr);
    }

    /**
     * 操作消息
     * @param $id 主键
     * @param $type 0:读取 1：删除
     * @return int
     */
    public function LogState()
    {
        $id = input('id',null);
        $type = input('type',0);
        $logs = Db::name('user_log')->where('ul_id',$id)->find();
        if(empty($logs)){
            AjaxReturn(-1,'消息获取错误');
        }
        if($type == 0){
            $res = Db::name('user_log')->where('ul_id',$id)->setField('ul_state',1);
        }else{
            $res = Db::name('user_log')->where('ul_id',$id)->delete();
        }
        if($res){
            AjaxReturn(1,'操作成功');
        }else{
            AjaxReturn(-1,'操作失败');
        }
    }

    /**
     *消息详情
     * @return object
     */
    public function logFind()
    {
        $id = input('id');
        $find = Db::name('user_log')->where('ul_id',$id)->find();
        $find['ul_time'] = date('Y-m-d H:i:s',$find['ul_time']);
        AjaxReturn(1,'ok',$find);
    }
    /**
     * 积分明细逻辑
     * @return array
     */
    public function getPoints()
    {
        //初始化数据
        $uid = Session::get('uid');

        $mlident = Db::name('money_log');
        $type = input('type',1);
        //获取相关数据
        $where['ml_uid'] = $uid;
        $find = $this->where('us_uid',$uid)->value('us_points');
        if($type == 1){
            $Spoints = $mlident->where($where)->where('ml_points','gt',0)->select();//总收入明细
        }else{
            $Spoints = $mlident->where($where)->where('ml_points','lt',0)->select();//总支出明细
        }
        foreach ($Spoints as $k=>$v){
            $Spoints[$k]['ml_time'] = date('Y-m-d H:i:s',$v['ml_time']);
            if($v['ml_points'] >= 0){
                $Spoints[$k]['ml_points'] = '+'.$v['ml_points'];
            }
        }
        if(empty($Spoints)){
            AjaxReturn(1,'暂时没有usdt的明细');
        }
        AjaxReturn(1,'ok',$Spoints);
    }
    /**
     * 我的团队页面
     * @return array
     */
    public function get_team()
    {
        $uid = Session::get('uid');
        $usident = Db::name('user');
        $find = $usident->where('us_uid', $uid)->find();
        $team = $usident->where('us_pid',$uid)->field('us_phone,us_atime')->select();//我的直推
        $team_direct = $usident->where('us_pid',$uid)->count();//直推人数
        if($team){
            foreach ($team as $k => $val){
                $team[$k]['us_atime'] = date('Y-m-d H:i:s',$val['us_atime']);
            }
        }
        $user = $usident->select();
        $team_num = GetTeamMember($user,$uid);//伞下人数
        $parent_find = $usident->where('us_uid',$find['us_pid'])->field('us_head,us_name')->find();//我的上级
        if($parent_find){
            $parent_find['us_head'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$parent_find['us_head'];
        }else{
            $parent_find = [];
        }
        $data['team'] = $team;
        $data['parent'] = $parent_find;
        $data['team_num'] = $team_num;
        $data['team_direct'] = $team_direct;
        if ($data) {
            AjaxReturn(1, '成功', $data);
        } else {
            AjaxReturn(-1, '未获取到会员信息');
        }
    }
    /**
     * 提现记录
     * @return array
     */
    public function getWithdrawal()
    {
        //初始化数据
        $uid = Session::get('uid');
        $mlident = Db::name('cash');
        $type = input('type',0);
        //获取相关数据
        $where['ca_uid'] = $uid;
        if($type == 0){
            $where['ca_state'] = $type;
            $Spoints = $mlident->where($where)->select();//总收入明细
        }else{
            $where['ca_state'] = array('>',0);
            $Spoints = $mlident->where($where)->select();//总支出明细
        }
        foreach ($Spoints as $k=>$v){
            $Spoints[$k]['ca_atime'] = date('Y-m-d H:i:s',$v['ca_atime']);

        }
        if(empty($Spoints)){
            AjaxReturn(1,'暂时没有提现记录');
        }
        AjaxReturn(1,'ok',$Spoints);
    }

}